
;(function($,window){
    function initPreviewBar(){
    	var $previewBar = this.query(this.opt.previewBar);
    	$previewBar.css('float', 'left');
    	var $galleryCells = this.query(this.opt.galleryCell);
         var barWidth = $galleryCells.length * (60 + 20);
         if (barWidth != 0) {
        	 $previewBar.css({ 'left': ((1000 - barWidth) / 2) + "px" });
         }
         else {
        	 $previewBar.css({ 'left': ((1000 - 60) / 2) + "px" });
         }
         
         var _this = this;
         var $previewCells = this.query(this.opt.previewCell);
         $.each($previewCells, function (index, item) {
             $(item).click(function (event) {
            	 _this.doSwipe(index, true);
                 return false;
             });
         });
         
         var $prevewButton = this.query(this.opt.previewButton);
         $prevewButton.mouseenter(function (event) {
        	 $prevewButton.animate({ 'bottom': '0px' }, 10);
         });
         $prevewButton.mouseleave(function (event) {
        	 $prevewButton.animate({ 'bottom': '-70px' }, 10);
         });
    }
    
    function initNavButton(){
    	
    	var startCell = this.opt.startCell;
    	var $leftButton = this.query(this.opt.nav.left);
    	var $rightButton = this.query(this.opt.nav.right);
		if (startCell == 0) {
            this.disabledLeft();
        }
		var $galleryCells = this.query(this.opt.galleryCell);
        if ($galleryCells.length == 0) {
        	$leftButton.hide();
        	$rightButton.hide();
        } else if ($galleryCells.length == 1) {
            this.disabledLeft();
            this.disabledRight();
        }
        else {
        	$rightButton.show();
        	$rightButton.show();
        }
        var _this = this;
        $leftButton.live("click", function () {
        	_this.toGo(-1, true);
            return false;
        });
        $rightButton.click(function (event) {
        	_this.toGo(1, true);
            return false;
        });
    }
	function init(){
		var $gallery = this.query(this.opt.gallery);
		if ($gallery.length > 0){
			//初始化导航按钮
			initNavButton.call(this);
			
	        //初始化预览bar
	        initPreviewBar.call(this);
	        
	        //计算gallery的宽度
	        var $galleryCells = this.query(this.opt.galleryCell);
	        this.galleryWidth = $($galleryCells[0]).width();
	        if (this.galleryWidth == 0) {
	        	//默认1000px
	        	this.galleryWidth = this.opt.defaultGalleryWidth;
	        }
	        this.lastAnimate = new Date().getTime();
		}
	};
	var Swipe = function($this,options){
		this.opt = {};
		$.extend(true,this.opt,Swipe.defaults,options);
		this.target = $this;
		init.call(this);
		
	};
	
	Swipe.defaults = {
			gallery:"#room_selectors_gallery",
			galleryCell:"#room_selectors_gallery .gallery_list li",
			nav:{
				'left': '.left',
		        'right': '.right'
			},
			previewButton:'#room_selectors_gallery div.more_photos',//morePhotoBlock
			previewBar:'#room_selectors_gallery div.photos_slider ul',
			previewCell:'#room_selectors_gallery div.photos_slider ul li',//bottomPhotos
			startCell:0,
			animating:false,
			paused:true,
			defaultGalleryWidth:1000
	};
	
	Swipe.prototype.query = function(selector){
		return $(selector,this.target);
	};
	
	Swipe.prototype.disabledLeft = function(){
		var $left = this.query(this.opt.nav.left);
		$left.removeClass("leftenabled");
		$left.removeAttr("href");
	};
	
	Swipe.prototype.disabledRight = function(){
		var $right = this.query(this.opt.nav.right);
		$right.removeClass("rightenabled");
		$right.removeAttr("href");
	};
	
	Swipe.prototype.enabledLeft = function(){
		var $left = this.query(this.opt.nav.left);
		$left.addClass("leftenabled");
		$left.attr("href", "javascript:;");
	};
	
	Swipe.prototype.enabledRight = function(){
		var $right = this.query(this.opt.nav.right);
		$right.addClass("rightenabled");
		$right.attr("href", "javascript:;");
	};
	
	Swipe.prototype.doSwipe = function (next,force){
		var curTime = new Date().getTime();
		if (((curTime - this.lastAnimate > 7000) || force) && !this.opt.animating){
			var $galleryCells = this.query(this.opt.galleryCell);
			var $galleryPanel = $galleryCells.parent();
			var gallerySize = $galleryCells.length;
			if (next > gallerySize - 1){
				next = gallerySize - 1;
				$galleryPanel.css('left',parseInt($galleryPanel.css('left')) + this.galleryWidth);
			}
			
			if (next < 0){
				next = 0;
				$galleryPanel.css('left', parseInt($galleryPanel.css('left')) - this.galleryWidth);
			}
			var nextLeft  = -next * this.galleryWidth;
			var _this = this;
			_this.opt.animating = true;
			$galleryPanel.animate({'left':nextLeft},500,function(){
				_this.opt.animating = false;
			});
			_this.opt.startCell = next;
			_this.lastAnimate = new Date().getTime();
			
			if (next == 0 && gallerySize > 1){
				this.disabledLeft();
				this.enabledRight();
			}
			
			if (next == gallerySize - 1 && gallerySize > 1){
				this.disabledRight();
				this.enabledLeft();
			}
		}
	};
	Swipe.prototype.toGo = function (direction,force){
		if (typeof force == 'undefined'){
			force = false;
		}
		var next = this.opt.startCell + direction;
		
		if (next == -1){
			if (this.opt.prePage){
				window.location.href = this.opt.prePage;
			}
			return;
		}
		
		if (next == this.query(this.opt.galleryCell).length){
			if (this.opt.nextPage){
        		window.location.href = this.opt.nextPage;
        	}
            return;
		}


        this.doSwipe(next, force);
	};
	
	$.fn.swipe = function(opt){
		var $this = $(this);
		$(this).each(function(i,ele){
			  // Return early if this element already has a plugin instance
			var $e = $(ele);
	        if ($e.data('swipe_plugin')) { return $e.data('swipe_plugin'); }
	        // Pass options to plugin constructor
	        var swipe = new Swipe($e,opt);
	        // Store plugin object in this element's data
	        $e.data('swipe_plugin', swipe);
		});
		return this;
	};
})(jQuery,window);
 